home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Atlanta_1990 / Atlanta-Devcon.1 / Libraries / Intuition / boopsi / demo2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  8.0 KB  |  336 lines

  1. /* demo2.c -- demonstration of         :ts=8
  2.  * Basic Object Oriented Programming System for Intuition
  3.  *
  4.  * This demo shows gadgets and images created using pre-defined
  5.  * boopsi classes, but not connected together or bound into a group.
  6.  * An improvement over demo1.c is that the buttons, slider, and string
  7.  * notify us using the new IDCMPUPDATE change mechanism for "real-time"
  8.  * updates of values.
  9.  */
  10.  
  11. /*
  12. Copyright (c) 1989 Commodore-Amiga, Inc.
  13.  
  14. Executables based on this information may be used in software
  15. for Commodore Amiga computers. All other rights reserved.
  16. This information is provided "as is"; no warranties are made.
  17. All use is at your own risk, and no liability or responsibility
  18. is assumed.
  19. */
  20.  
  21. #include "sysall.h"
  22. #include <intuition/icclass.h>
  23.  
  24. #define D(x)    ;
  25.  
  26. struct  IntuitionBase   *IntuitionBase;
  27. struct  GfxBase         *GfxBase;
  28. struct  Library         *UtilityBase;
  29.  
  30. /* from varargs.c -- interface to NewObjectA()    */
  31. Object    *NewObject();
  32.  
  33. ULONG    myiflags =  CLOSEWINDOW | IDCMPUPDATE;
  34.  
  35. struct Gadget    *propg;
  36. struct Gadget    *stringg;
  37. struct Gadget    *uparrowg;
  38. struct Gadget    *downarrowg;
  39.  
  40. /* pictures for arrows    */
  41. struct Image    *upimage;
  42. struct Image    *downimage;
  43.  
  44. /* some static layout and setup constants, for now    */
  45. #define GTOP        (44)
  46. #define ARROWLEFT    (180)
  47. #define PWIDTH        (120)    /* width of horizontal propslider    */
  48. #define SWIDTH        (50)
  49. #define PROPRANGE    (20)
  50. #define INITVAL        (0)    /* initial value of string and slider    */
  51.  
  52. enum gadgetids {
  53.     gUp = 1,
  54.     gDown,
  55.     gSlider,
  56.     gString,
  57. };
  58.  
  59. #define MYTAG_CURRVAL    (TAG_USER + 999)
  60.  
  61. /* mapping tag lists for IDCMPUPDATE    */
  62. struct TagItem    slider2me[] = {
  63.     {GA_ID, GA_ID},
  64.     {PGA_TOP, MYTAG_CURRVAL},
  65.     {TAG_END, }
  66. };
  67.  
  68. struct TagItem    arrows2me[] = {
  69.     {GA_ID, GA_ID},
  70.     {TAG_END, }
  71. };
  72.  
  73. struct TagItem    string2me[] = {
  74.     {GA_ID, GA_ID},
  75.     {STRINGA_LongVal, MYTAG_CURRVAL},
  76.     {TAG_END, }
  77. };
  78.  
  79. struct TagItem    proptags[] = {
  80.     {GA_TOP,        GTOP},
  81.     {GA_WIDTH,        PWIDTH},    /* height to be specified    */
  82.     {GA_ID,        gSlider},
  83.  
  84.     {ICA_TARGET,    ICTARGET_IDCMP},
  85.     {ICA_MAP,        (ULONG) &slider2me[0]},
  86.  
  87.     {PGA_FREEDOM,    FREEHORIZ},
  88.     {PGA_TOP,        INITVAL},    /* "top" in the scroller sense    */
  89.     {PGA_VISIBLE,    1},        /* want an integer value slider    */
  90.     {PGA_TOTAL,        PROPRANGE},
  91.  
  92.     {TAG_END ,}
  93. };
  94.  
  95. struct TagItem    stringtags[] = {
  96.     {GA_ID,        gString},
  97.     {GA_TOP,        GTOP},
  98.     {GA_WIDTH,        SWIDTH},
  99.     {GA_HEIGHT,        12},        /* fix this    */
  100.  
  101.     {ICA_TARGET,    ICTARGET_IDCMP},
  102.     {ICA_MAP,        (ULONG) &string2me[0]},
  103.  
  104.     {STRINGA_MaxChars,    10},
  105.     {STRINGA_LongVal,    INITVAL},    /* make it an integer gadget */
  106.     {STRINGA_Justification,
  107.                 STRINGRIGHT},
  108.     {TAG_END, }
  109. };
  110.  
  111. struct TagItem    arrowtags[] = {
  112.     {GA_TOP,        GTOP},
  113.  
  114.     {ICA_TARGET,    ICTARGET_IDCMP},
  115.     {ICA_MAP,        (ULONG) &arrows2me[0]},
  116.     {TAG_END, }
  117. };
  118.  
  119. #define MYWINDOWTITLE    "boopsi Demo 2"
  120.  
  121. main()
  122. {
  123.     struct DrawInfo    *GetScreenDrawInfo();
  124.     struct Window     *OpenWindowTags();    /* in varargs.c for now */
  125.  
  126.     struct Gadget    *mygadgets = NULL;
  127.     struct Gadget    *tmpgad;
  128.     struct Window    *w;
  129.     struct DrawInfo    *drinfo;
  130.  
  131.     if (!(UtilityBase=(struct Library *)OpenLibrary("utility.library",36L)))
  132.     { cleanup("no V36 utility library\n"); }
  133.  
  134.     if (!(IntuitionBase = 
  135.     (struct IntuitionBase *) OpenLibrary("intuition.library", 36L)))
  136.     { cleanup("no V36 intuition library\n"); }
  137.  
  138.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36L)))
  139.     { cleanup("no V36 graphics library\n"); }
  140.  
  141.     D( printf("about to openwindow\n") );
  142.     w = OpenWindowTags( NULL, 
  143.         WA_Title,    MYWINDOWTITLE,
  144.         WA_SimpleRefresh, TRUE,
  145.         WA_NoCareRefresh, TRUE,
  146.         WA_DepthGadget,    TRUE,
  147.         WA_DragBar,    TRUE,
  148.         WA_Left,    300,
  149.         WA_Top,        50,
  150.         WA_Width,    280,
  151.         WA_Height,    120,
  152.         WA_IDCMP,    myiflags,
  153.         WA_CloseGadget,    TRUE,
  154.             TAG_END );
  155.     D( printf("window at %lx\n ", w ) );
  156.  
  157.     if ( w == NULL) cleanup( "couldn't open my window.\n");
  158.     drinfo = GetScreenDrawInfo( w->WScreen );
  159.  
  160.     /* get images for the up and down arrows, sensitive
  161.      * to depth and pen specs for current screen (we'll be
  162.      * adding resolution/size selection later).
  163.      */
  164.     upimage = (struct Image *) NewObject( NULL, "sysiclass",
  165.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  166.     SYSIA_Which,    UPIMAGE,
  167.     SYSIA_DrawInfo,    drinfo,
  168.         TAG_END );
  169.  
  170.     downimage = (struct Image *) NewObject( NULL, "sysiclass",
  171.     SYSIA_Size,    0,        /* normal "medium-res" for now */
  172.     SYSIA_Which,    DOWNIMAGE,
  173.     SYSIA_DrawInfo,    drinfo,
  174.         TAG_END );
  175.  
  176.     /* make gadgets, link into list (easier starting with Beta 4) */
  177.     tmpgad = (struct Gadget *) &mygadgets;
  178.  
  179.     downarrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  180.     GA_IMAGE,     downimage,
  181.     GA_LEFT,    ARROWLEFT,
  182.     GA_ID,        gDown,
  183.     GA_PREVIOUS,    tmpgad,
  184.     TAG_MORE,    arrowtags,
  185.  
  186.     TAG_END );
  187.     D( printf("downgadget at %lx\n", downarrowg ));
  188.  
  189.     /* get up/down arrow button gadgets    */
  190.     uparrowg = (struct Gadget *) NewObject( NULL, "buttongclass",
  191.     GA_IMAGE,     upimage,
  192.     GA_LEFT,    ARROWLEFT + (downarrowg? downarrowg->Width: 0),
  193.     GA_ID,        gUp,
  194.     GA_PREVIOUS,    tmpgad,
  195.     TAG_MORE,    arrowtags,
  196.     TAG_END );
  197.     D( printf("upgadget at %lx\n", uparrowg ));
  198.  
  199.  
  200.     propg = (struct Gadget *) NewObject( NULL, "propgclass",
  201.     GA_LEFT,    ARROWLEFT - PWIDTH,
  202.     GA_HEIGHT,    downarrowg? downarrowg->Height: 20,
  203.     GA_PREVIOUS,    tmpgad,
  204.     TAG_MORE,    proptags,
  205.     TAG_END );
  206.     D( printf( "prop gadget returned: %lx\n", propg ) );
  207.  
  208.     stringg  = (struct Gadget *) NewObject( NULL, "strgclass",
  209.         GA_LEFT,    propg? (propg->LeftEdge - SWIDTH): 20,
  210.     GA_PREVIOUS,    tmpgad,
  211.     TAG_MORE,    stringtags,
  212.     TAG_END );
  213.     D( printf( "string at %lx\n", stringg ) );
  214.  
  215.     D(printf("objects initialized\n"));
  216.  
  217.     AddGList( w, mygadgets, -1, -1, NULL );
  218.     RefreshGList( mygadgets, w, NULL, -1 );
  219.  
  220.     D( printf("gadgets added and refreshed \n") );
  221.  
  222.     goHandleWindow( w );
  223.  
  224.     RemoveGList( w, mygadgets, -1 );
  225.     FreeScreenDrawInfo( w->WScreen, drinfo );
  226.     CloseWindow( w );
  227.  
  228.     D( printf("dispose\n") );
  229.     while ( mygadgets )
  230.     {
  231.     tmpgad = mygadgets->NextGadget;
  232.     DisposeObject( mygadgets );
  233.     mygadgets = tmpgad;
  234.     }
  235.  
  236.     DisposeObject( upimage);
  237.     DisposeObject( downimage);
  238.     D( printf("have disposed.\n") );
  239.  
  240.     cleanup( "all done" );
  241. }
  242.  
  243. cleanup( str )
  244. char    *str;
  245. {
  246.     if (str) printf("%s\n", str);
  247.  
  248.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  249.     if (GfxBase) CloseLibrary(GfxBase);
  250.     if (UtilityBase) CloseLibrary(UtilityBase);
  251.  
  252.     exit (0);
  253. }
  254.  
  255. /* this variable holds the integer "current value" of the
  256.  * whole little system of gadgets
  257.  */
  258. LONG        currval = 0;
  259.  
  260. goHandleWindow( w )
  261. struct Window    *w;
  262. {
  263.     struct TagItem    *FindTagItem();
  264.  
  265.     struct IntuiMessage *imsg;
  266.     struct TagItem    *tags;
  267.     /* struct TagItem    *ti; */
  268.     ULONG        gid;
  269.  
  270.     for(;;)
  271.     {
  272.     WaitPort( w->UserPort );
  273.     while ( imsg = (struct IntuiMessage *) GetMsg( w->UserPort ) )
  274.     {
  275.         switch ( imsg->Class )
  276.         {
  277.         case CLOSEWINDOW:
  278.             ReplyMsg( (struct Message *) imsg );
  279.         return;
  280.  
  281.         case IDCMPUPDATE:
  282.         tags = (struct TagItem *) imsg->IAddress;
  283.             D( printf("idcmpupdate, tags at %lx quals %lx\n",
  284.             tags, imsg->Qualifier ));
  285.         D( printf("gadget id %lx\n", GetTagData( GA_ID, 0, tags )));
  286.  
  287.         switch (  gid = GetTagData( GA_ID, 0, tags ) )
  288.         {
  289.         case gString:
  290.         case gSlider:
  291.             currval =  GetTagData( MYTAG_CURRVAL, 0, tags );
  292.             manualUpdate( w, gid );
  293.             break;
  294.  
  295.         case gUp:
  296.             currval++;
  297.             manualUpdate( w, 0 );
  298.             break;
  299.         case gDown:
  300.             currval--;
  301.             manualUpdate( w, 0 );
  302.             break;
  303.             /* cases where button GA_ID's are negative are 
  304.              * repeat codes when the pointer is OFF
  305.              * the button, and are ignored
  306.              */
  307.         }
  308.         break;
  309.         }
  310.         ReplyMsg( (struct Message *) imsg );
  311.     }
  312.     }
  313. }
  314.  
  315. manualUpdate( w, activeid )
  316. struct Window    *w;
  317. {
  318.     /* put currval in legal bounds    */
  319.     currval = max( 0, min( PROPRANGE-1, currval ) );
  320.  
  321.     /* tell interested parties, but not active slider        */
  322.     if ( activeid != gSlider )
  323.     {
  324.     SetGadgetAttrs( propg, w, NULL, 
  325.         PGA_TOP, currval,
  326.         TAG_END );
  327.     }
  328.  
  329.     /* always OK to update string, and it needs limited currval
  330.      * to boot.
  331.      */
  332.     SetGadgetAttrs( stringg, w, NULL, 
  333.     STRINGA_LongVal, currval,
  334.     TAG_END );
  335. }
  336.